﻿using System.Collections.Generic;
using System.Windows.Forms;
using NUnit.Framework;

namespace ExtensionMethodExtravaganza.Tests
{
	[TestFixture]
	public class TreeViewExtensionsTests
	{
		[Test]
		public void FindNodeInTreeViewTest()
		{
			var names = new[] { "a", "a.a", "a.a.a", "b", "b.b", "b.b.b" };
			
			var treeView = new TreeView();
			var a = treeView.Nodes.Add(names[0]);
			var aa = a.Nodes.Add(names[1]);
			var aaa = aa.Nodes.Add(names[2]);
			var b = treeView.Nodes.Add(names[3]);
			var bb = b.Nodes.Add(names[4]);
			var bbb = bb.Nodes.Add(names[5]);

			for (var i = 0; i < names.Length; i++)
			{
				var name = names[i];
				var foundNode = treeView.FindNode(n => n.Text == name);
				Assert.IsNotNull(foundNode);
				Assert.AreEqual(name, foundNode.Text);
			}
		}

		[Test]
		public void FindNodeFromTreeNodeTest()
		{
			var names = new[] { "a", "a.a", "a.a.a", "b", "b.b", "b.b.b" };
			
			var treeView = new TreeView();
			var a = treeView.Nodes.Add(names[0]);
			var aa = a.Nodes.Add(names[1]);
			var aaa = aa.Nodes.Add(names[2]);
			var b = treeView.Nodes.Add(names[3]);
			var bb = b.Nodes.Add(names[4]);
			var bbb = bb.Nodes.Add(names[5]);

			Assert.AreSame(aaa, a.FindNode(n => n.Text == "a.a.a"));
			Assert.AreSame(bbb, b.FindNode(n => n.Text == "b.b.b"));

			Assert.IsNull(b.FindNode(n => n.Text == "a.a.a"));
			Assert.IsNull(a.FindNode(n => n.Text == "b.b.b"));
		}

		[Test]
		public void FindNodesInTreeViewTest()
		{
			var names = new[] { "a", "a.a", "a.a.a", "b", "b.b", "b.b.b" };
			
			var treeView = new TreeView();
			var a = treeView.Nodes.Add(names[0]);
			var aa = a.Nodes.Add(names[1]);
			var aaa = aa.Nodes.Add(names[2]);
			var b = treeView.Nodes.Add(names[3]);
			var bb = b.Nodes.Add(names[4]);
			var bbb = bb.Nodes.Add(names[5]);

			var childList = new List<TreeNode>(a.FindNodes(n => n.Text.StartsWith("a.a")));

			Assert.AreSame(aa, childList[0]);
			Assert.AreSame(aaa, childList[1]);

			childList = new List<TreeNode>(b.FindNodes(n => n.Text.StartsWith("b.b")));

			Assert.AreSame(bb, childList[0]);
			Assert.AreSame(bbb, childList[1]);
		}

		[Test]
		public void FindNodesFromTreeNodeTest()
		{
			var names = new[] { "a", "a.a", "a.a.a", "b", "b.b", "b.b.b" };
			
			var treeView = new TreeView();
			var a = treeView.Nodes.Add(names[0]);
			var aa = a.Nodes.Add(names[1]);
			var aaa = aa.Nodes.Add(names[2]);
			var b = treeView.Nodes.Add(names[3]);
			var bb = b.Nodes.Add(names[4]);
			var bbb = bb.Nodes.Add(names[5]);

			var childList = new List<TreeNode>(a.FindNodes(n => n.Text.StartsWith("a.a")));

			Assert.AreSame(aa, childList[0]);
			Assert.AreSame(aaa, childList[1]);

			childList = new List<TreeNode>(b.FindNodes(n => n.Text.StartsWith("b.b")));

			Assert.AreSame(bb, childList[0]);
			Assert.AreSame(bbb, childList[1]);
		}

		[Test]
		public void GetDescendantNodesFromTreeNodeTest()
		{
			var names = new[] { "a", "a.a", "a.a.a", "b", "b.b", "b.b.b" };
			
			var treeView = new TreeView();
			var a = treeView.Nodes.Add(names[0]);
			var aa = a.Nodes.Add(names[1]);
			var aaa = aa.Nodes.Add(names[2]);
			var b = treeView.Nodes.Add(names[3]);
			var bb = b.Nodes.Add(names[4]);
			var bbb = bb.Nodes.Add(names[5]);

			var descendants = a.GetDescendantNodes();
			Assert.AreEqual(2, descendants.Count);
			Assert.Contains(aa, descendants);
			Assert.Contains(aaa, descendants);

			descendants = b.GetDescendantNodes();
			Assert.AreEqual(2, descendants.Count);
			Assert.Contains(bb, descendants);
			Assert.Contains(bbb, descendants);
		}

		[Test]
		public void GetDescendantNodesFromTreeViewTest()
		{
			var names = new[] { "a", "a.a", "a.a.a", "b", "b.b", "b.b.b" };
			
			var treeView = new TreeView();
			var a = treeView.Nodes.Add(names[0]);
			var aa = a.Nodes.Add(names[1]);
			var aaa = aa.Nodes.Add(names[2]);
			var b = treeView.Nodes.Add(names[3]);
			var bb = b.Nodes.Add(names[4]);
			var bbb = bb.Nodes.Add(names[5]);

			var descendants = treeView.GetDescendantNodes();
			Assert.AreEqual(6, descendants.Count);
			Assert.Contains(a, descendants);
			Assert.Contains(aa, descendants);
			Assert.Contains(aaa, descendants);
			Assert.Contains(b, descendants);
			Assert.Contains(bb, descendants);
			Assert.Contains(bbb, descendants);
		}
	}
}
